//
//  SCImagePickerView.m
//  Shuaiche
//
//  Created by zzs on 2018/8/10.
//  Copyright © 2018年 zzs. All rights reserved.
//

#import "SCImagePickerView.h"
#import "SCImagePickerCell.h"
#import "MBProgressHUD+Add.h"

@interface SCImagePickerView()<SCImagePickerCellDelegate>
@end
@implementation SCImagePickerView

+ (instancetype)sharePickerViewWithFrame:(CGRect)frame countOfRow:(NSInteger)count{
    SCImagePickerView *imagePickerV = [[SCImagePickerView alloc]initWithFrame:frame countOfRow:count];
    return imagePickerV;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
     SCImagePickerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:SCImagePickerCellIdentifier forIndexPath:indexPath];
    cell.delegate = self;
    [self configureCell:(SCImagePickerCell *)cell atIndexPath:indexPath];
    return cell;
}

- (void)configureCell:(SCImagePickerCell *)cell atIndexPath:(NSIndexPath *)indexPath{
    cell.cellIndexPath = indexPath;
    if (indexPath.item == self.mediaArray.count) {
        cell.pickerImageView.image = [UIImage imageNamed:@"LLImagePicker.bundle/AddMedia" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil];
        cell.pickerBtn.hidden = YES;
    }else{
        LLImagePickerModel *model = self.mediaArray[indexPath.item];
        cell.isItemSelected = model.isSelected;
        if(model.isSelected){
            [cell.pickerBtn setImage:[UIImage imageNamed:@"agent_check"] forState:UIControlStateNormal];
        }else{
             [cell.pickerBtn setImage:[UIImage imageNamed:@"agent_uncheck"] forState:UIControlStateNormal];
        }
        if (!model.isVideo && model.imageUrlString) {
            [cell.pickerImageView ll_setImageWithUrlString:model.imageUrlString placeholderImage:nil];
        }else {
            cell.pickerImageView.image = model.image;
        }
        cell.pickerBtn.hidden = NO;
    }
}

- (BOOL)pickerViewDidSelectedCell:(SCImagePickerCell *)cell atIndexPath:(NSIndexPath *)indexPath{
    //注意如果换了位置此时的indexPath不正确,所以重算
    NSIndexPath *realIndexPath = [self.collectionView indexPathForCell:cell];
    LLImagePickerModel *model = self.mediaArray[realIndexPath.item];
    if(self.selectedSharedImages.count>=self.maxSharedNum){
        if(!model.isSelected){
         [MBProgressHUD showError:[NSString stringWithFormat:@"最多选择%@张图",@(self.maxSharedNum)] toView:nil];
            return NO;
        }
    }
    model.isSelected = !model.isSelected;
    if(model.isSelected){
        if(![self.selectedSharedImages containsObject:model]){
            [self.selectedSharedImages addObject:model];
            if(self.observerdSharedImages){
                self.observerdSharedImages(self.selectedSharedImages);
            }
        }
    }else{
        if([self.selectedSharedImages containsObject:model]){
            [self.selectedSharedImages removeObject:model];
            if(self.observerdSharedImages){
                self.observerdSharedImages(self.selectedSharedImages);
            }
        }
    }
    return YES;
}

- (void)handleMovingPress:(UILongPressGestureRecognizer *)longPress{
    switch (longPress.state) {
        case UIGestureRecognizerStateBegan: {
            {
                NSIndexPath *selectIndexPath = [self.collectionView indexPathForItemAtPoint:[longPress locationInView:self.collectionView]];
                if (selectIndexPath == nil){
                    return;
                }
                [self.collectionView beginInteractiveMovementForItemAtIndexPath:selectIndexPath];
            }
            break;
        }
        case UIGestureRecognizerStateChanged: {
            [self.collectionView updateInteractiveMovementTargetPosition:[longPress locationInView:longPress.view]];
            break;
        }
        case UIGestureRecognizerStateEnded: {
            NSIndexPath *selectIndexPath = [self.collectionView indexPathForItemAtPoint:[longPress locationInView:self.collectionView]];
            if((selectIndexPath.row >= self.mediaArray.count&&self.mediaArray.count<self.maxImageSelected)||!selectIndexPath){
                [self.collectionView cancelInteractiveMovement];
            }else{
                [self.collectionView endInteractiveMovement];
            }
            break;
        }
        default:{
            [self.collectionView cancelInteractiveMovement];
        }
            break;
    }
}

- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser actionButtonPressedForPhotoAtIndex:(NSUInteger)index;{
    
}



@end
